package com.qdlc.p2p.biz.adapted.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.qd.p2p.adapter.AdaptePlatform;
import com.qd.p2p.adapter.AdaptedRequestHandleService;
import com.qd.p2p.adapter.param.RechargeByAuthenticationParameter;
import com.qd.p2p.adapter.param.RechargeByPageParameter;
import com.qd.p2p.adapter.response.HtmlResponse;
import com.qdlc.p2p.biz.processor.event.RechargeValueEvent;
import com.qdlc.p2p.biz.processor.task.RechargeSuccessTaskProcessor;
import com.qdlc.p2p.biz.service.AccountRechargeService;
import com.qdlc.p2p.biz.util.NotifyRequestHandler;
import com.qdlc.p2p.biz.util.NotifyRequestUrl;
import com.qdlc.p2p.common.constant.Constant;
import com.qdlc.p2p.common.constant.RechargeConstant;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import com.qdlc.p2p.common.util.RandomUtil;
import com.qdlc.p2p.dal.dto.AccountRecharge;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.AccountRechargeModel;

@Service
public class AdaptedBaofooRechargeService extends AbstractAdaptedRechargeService{

    @Resource
    private AccountRechargeService accountRechargeService;
    
    @Resource
    AdaptedRequestHandleService adaptedRequestHandleService;
	
	@Override
	public ReturnResult doRecharge(User user, double money, HttpServletRequest request) {
    	
		HtmlResponse rechargeHtmlResponse = null;
		
		//支付方式[网银支付 online_pay,认证支付 auth_pay]
		String mode = request.getParameter("recharge_mode");
		String orderId = RandomUtil.getRandomNumString(8);
		//充值接口请求
		if("auth_pay".equals(mode)){
			RechargeByAuthenticationParameter authenticationParameter = new RechargeByAuthenticationParameter();
			authenticationParameter.setPlatformUserId(user.getTppUserCustId());
			authenticationParameter.setAmount(money);
			authenticationParameter.setOrderId(orderId);
			authenticationParameter.setPageUrl(NotifyRequestUrl.RECHARGE_PAGE_NOTIFY_URL);
			authenticationParameter.setReturnUrl(NotifyRequestUrl.RECHARGE_RETURN_NOTIFY_URL);
			rechargeHtmlResponse = adaptedRequestHandleService.rechargeByAuthentication(authenticationParameter);
		}else{
			RechargeByPageParameter parameter = new RechargeByPageParameter();
			parameter.setPlatformUserId(user.getTppUserCustId());
			parameter.setAmount(money);
			parameter.setOrderId(orderId);
			parameter.setPageUrl(NotifyRequestUrl.RECHARGE_PAGE_NOTIFY_URL);
			parameter.setReturnUrl(NotifyRequestUrl.RECHARGE_RETURN_NOTIFY_URL);
			rechargeHtmlResponse = adaptedRequestHandleService.rechargeByPage(parameter);
		}
		
		if(rechargeHtmlResponse == null){
			return ReturnResult.SERVER_BUSY;
		}
    	if(!rechargeHtmlResponse.isSuccess()){
    		return new ReturnResult(rechargeHtmlResponse.getResponseMsg());
    	}
    	
    	//请求成功后，保存充值记录
    	AccountRecharge ar = new AccountRecharge(orderId, user, money, RechargeConstant.PAY_ONLINE_TYPE, Constant.TPP_NAME);
    	accountRechargeService.add(ar);
    	
		ReturnResult result = new ReturnResult();
		result.addExtra("html", rechargeHtmlResponse.getHtml());
    	return result;
	}

	@Override
	public AdaptePlatform supportPlatform() {
		return AdaptePlatform.BAOFOO;
	}

	@Override
	public ReturnResult notifying(HttpServletRequest request, HttpServletResponse response) {
        //获取充值后的通知请求
        NotifyRequestHandler notifyReqeust = NotifyRequestHandler.build(request);
        //构造队列事件参数，根据充值状态来执行成功/失败任务
        String rechargeNotifyResponse = notifyReqeust.getParam("result");
        RechargeValueEvent valueEvent = new RechargeValueEvent();
        valueEvent.setNotifyResponse(rechargeNotifyResponse);
        valueEvent.setTaskProcessorClass(RechargeSuccessTaskProcessor.class);
        //将事件放入到队列中
        QueueHandlers.put(valueEvent);
        return ReturnResult.SUCCESS;
	}

	@Override
	public ReturnResult authRecharge(User user, double money, HttpServletRequest request) {
		HtmlResponse rechargeHtmlResponse = null;

		//支付方式[网银支付 online_pay,认证支付 auth_pay]
		String orderId = RandomUtil.getRandomNumString(8);
		//充值接口请求
		RechargeByAuthenticationParameter authenticationParameter = new RechargeByAuthenticationParameter();
		authenticationParameter.setPlatformUserId(user.getTppUserCustId());
		authenticationParameter.setAmount(money);
		authenticationParameter.setOrderId(orderId);
		authenticationParameter.setPageUrl(NotifyRequestUrl.WEIXIN_RECHARGE_PAGE_NOTIFY_URL);
		authenticationParameter.setReturnUrl(NotifyRequestUrl.RECHARGE_RETURN_NOTIFY_URL);
		rechargeHtmlResponse = adaptedRequestHandleService.rechargeByAuthentication(authenticationParameter);

		if(rechargeHtmlResponse == null){
			return ReturnResult.SERVER_BUSY;
		}
		if(!rechargeHtmlResponse.isSuccess()){
			return new ReturnResult(rechargeHtmlResponse.getResponseMsg());
		}

		//请求成功后，保存充值记录
		AccountRecharge ar = new AccountRecharge(orderId, user, money, RechargeConstant.PAY_ONLINE_TYPE, Constant.TPP_NAME);
		accountRechargeService.add(ar);

		ReturnResult result = new ReturnResult();
		result.addExtra("html", rechargeHtmlResponse.getHtml());
		return result;
	}

}
